package com.tianhui.consignortwofei.mvp.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.fgs.common.CommonFragment;
import com.fgs.common.CommonPresenter;
import com.fgs.common.CommonResponse;
import com.fgs.common.CommonView;
import com.fgs.common.imageView.PictureDialog;
import com.fgs.common.progress.ExceptionHandle;
import com.fgs.common.progress.ObserverResponseListener;
import com.fgs.common.utils.ToastUtil;
import com.tianhui.consignortwofei.R;
import com.tianhui.consignortwofei.base.BaseActivity;
import com.tianhui.consignortwofei.constant.KeyValue;
import com.tianhui.consignortwofei.mvp.model.OrderModel;
import com.tianhui.consignortwofei.mvp.model.enty.DisplayPictureInfo;
import com.tianhui.consignortwofei.mvp.model.enty.DriverInfo;
import com.tianhui.consignortwofei.mvp.model.enty.OrderInfo;
import com.tianhui.consignortwofei.mvp.ui.adapter.DisplayPictureAdapter;

import org.yczbj.ycrefreshviewlib.inter.OnItemClickListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

/**
 * @author fgs
 * @date 2020/9/2
 * @description
 **/
public class DriverInfoActivity extends BaseActivity {

    @BindView(R.id.activity_driver_info_pictureRecyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.activity_driver_info_nameTextView)
    TextView mNameTextView;
    //信用积分
    @BindView(R.id.activity_driver_info_integralTextView)
    TextView mIntegralTextView;
    //车牌号
    @BindView(R.id.activity_driver_info_plateNumberTextView)
    TextView mPlateNumber;
    //联系方式
    @BindView(R.id.activity_driver_info_phoneTextView)
    TextView mPhoneTextView;
    //所属承运商
    @BindView(R.id.activity_driver_info_ownedCarrierTextView)
    TextView mOwnedCarrierTextView;
    //结算结象
    @BindView(R.id.activity_driver_info_settlementCarrierTextView)
    TextView mSettlementCarrierTextView;

    //详细地址
    @BindView(R.id.activity_driver_info_addressTextView)
    TextView mAddressTextView;
    //驾龄
    @BindView(R.id.activity_driver_info_drivingYearTextView)
    TextView mDrivingYearTextView;
    //准驾类型
    @BindView(R.id.activity_driver_info_drivingTypeTextView)
    TextView mDrivingTypeTextView;
    //从业资格证号
    @BindView(R.id.activity_driver_info_qualificationNumberTextView)
    TextView mQualificationNumberTypeTextView;
    //驾驶证发证机关
    @BindView(R.id.activity_driver_info_issueLicenseAuthorityTextView)
    TextView mIssueLicenseAuthorityTypeTextView;
    //驾驶证号码
    @BindView(R.id.activity_driver_info_drivingNumberTextView)
    TextView mDrivingNumberTypeTextView;
    //驾驶证有效期
    @BindView(R.id.activity_driver_info_drivingDateTextView)
    TextView mDrivingDateTypeTextView;
    //身份证有效期
    @BindView(R.id.activity_driver_info_idCardDateTextView)
    TextView mIdCardDateTypeTextView;
    //从业资格证有效期
    @BindView(R.id.activity_driver_info_qualificationDateTextView)
    TextView mQualificationDateTypeTextView;


    private DisplayPictureAdapter pictureAdapter;

    private OrderModel orderModel;

    @Override
    public int getLayoutId() {
        return R.layout.activity_driver_info;
    }

    @Override
    public CommonPresenter createPresenter() {
        return null;
    }

    @Override
    public CommonView createView() {
        return null;
    }

    @Override
    public String getToolbarTitle() {
        return "司机信息";
    }

    @Override
    public void init() {

        orderModel = new OrderModel();


        pictureAdapter = new DisplayPictureAdapter(this);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);
        recyclerView.setLayoutManager(gridLayoutManager);
        recyclerView.setAdapter(pictureAdapter);

        pictureAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                DisplayPictureInfo displayPictureInfo = pictureAdapter.getAllData().get(position);

                if (!TextUtils.isEmpty(displayPictureInfo.imageId)) {
                    PictureDialog pictureDialog = new PictureDialog(DriverInfoActivity.this);
                    pictureDialog.setImageId(displayPictureInfo.imageId);
                    pictureDialog.show();
                } else {
                    showToast(R.string.tip_without_upload_picture);
                }

            }
        });


        Bundle bundle = getIntent().getExtras();
        if (bundle != null && !bundle.isEmpty()) {
            OrderInfo orderInfo = bundle.getParcelable(KeyValue.KEY_ORDER_INFO);
            if (orderInfo != null && !TextUtils.isEmpty(orderInfo.driverid)) {
                String driverId = orderInfo.driverid;
                Map<String, String> map = new HashMap<>();
                map.put("id", driverId);
                orderModel.driverInfoModel(this, map, true, this.bindLifecycle(), new ObserverResponseListener<CommonResponse<DriverInfo>>() {
                    @Override
                    public void onNext(CommonResponse<DriverInfo> commonResponse) {
                        if (commonResponse.getCode() == 20000) {
                            DriverInfo driverInfo = commonResponse.getData();
                            if (driverInfo != null) {
                                showDriverInfo(driverInfo);
                            }

                        } else {
                            ToastUtil.showShortToast(commonResponse.getMsg());
                        }

                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseThrowable e) {
                        ToastUtil.showShortToast(e.message);
                    }
                });
            } else {
                showToast("获取司机信息失败");
            }
        }

    }

    private void showDriverInfo(DriverInfo driverInfo) {
        mNameTextView.setText("姓名：" + driverInfo.drivername);
        mPlateNumber.setText("车牌号：" + driverInfo.defaultvehiclenum);
        mIntegralTextView.setText(driverInfo.creditscore);
        mPhoneTextView.setText("联系方式：" + driverInfo.drivertel);
        if (!TextUtils.isEmpty(driverInfo.carriername)) {
            mOwnedCarrierTextView.setText("所属承运商：" + driverInfo.carriername);
        } else {
            mOwnedCarrierTextView.setText("所属承运商：" + "个人");
        }
        mSettlementCarrierTextView.setText("结算对象：" + "个人");
        mAddressTextView.setText(driverInfo.driverprovince + driverInfo.drivercity + driverInfo.drivercounty + driverInfo.orderplacedetails);
        mDrivingYearTextView.setText("驾龄：" + driverInfo.driverage);
        mDrivingTypeTextView.setText("准驾类型：" + driverInfo.transporttypename);
        mQualificationNumberTypeTextView.setText("从业资格证号：" + driverInfo.qualificationnumber);
        mIssueLicenseAuthorityTypeTextView.setText("驾驶证发证机关：" + driverInfo.issuingorganizations);
        mDrivingNumberTypeTextView.setText("驾驶证号码：" + driverInfo.driverlicensenumber);
        mDrivingDateTypeTextView.setText("驾驶证有效期：" + driverInfo.validperiodfrom + "--" + driverInfo.driverlicenseendtime);
        mIdCardDateTypeTextView.setText("身份证有效期：" + driverInfo.idcardendtime);
        mQualificationDateTypeTextView.setText("从业资格证有效期：" + driverInfo.qualificationendtime);
        pictureAdapter.addAll(getPictureInfoList(driverInfo));

    }

    private List<DisplayPictureInfo> getPictureInfoList(DriverInfo driverInfo) {
        List<DisplayPictureInfo> list = new ArrayList<>();


        DisplayPictureInfo displayPictureInfo;
        displayPictureInfo = new DisplayPictureInfo();
//        displayPictureInfo.title = "身份证正面";
        displayPictureInfo.imageId = driverInfo.idcardtphoto;
//        displayPictureInfo.imageId = "8238";
//        displayPictureInfo.imageId = "8238,8239,8240,8241";
        list.add(displayPictureInfo);

        displayPictureInfo = new DisplayPictureInfo();
//        displayPictureInfo.title = "身份证反面";
        displayPictureInfo.imageId = driverInfo.idcardfphoto;
//        displayPictureInfo.imageId = "8239";
        list.add(displayPictureInfo);

        displayPictureInfo = new DisplayPictureInfo();
//        displayPictureInfo.title = "驾驶证正面";
        displayPictureInfo.imageId = driverInfo.driverlicense;
//        displayPictureInfo.imageId = "8240";
        list.add(displayPictureInfo);

        displayPictureInfo = new DisplayPictureInfo();
//        displayPictureInfo.title = "从业资格证正面";
        displayPictureInfo.imageId = driverInfo.qualification;
//        displayPictureInfo.imageId = "8241";
        list.add(displayPictureInfo);

        return list;
    }

    @Override
    public CommonFragment createFragment() {
        return null;
    }
}
